#encoding=utf-8

#import wave
import urllib
import urllib2
#import pycurl
#import base64
import json
import os
## get access token by api key & secret key

def get_token():
	apiKey = "Aoz1wQwMpnMTAvjMU74eItvA"
	secretKey = "84be0fbcb12692ab55daced0aa43cb4e"

	auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;  

	res = urllib2.urlopen(auth_url)
	json_data = res.read()
#	print json_data
	return json.loads(json_data)['access_token']


if __name__ == "__main__":
	token = get_token()
	texta = raw_input("Enter your input: ")
	textb = urllib.quote(texta)
	url = "http://tsn.baidu.com/text2audio?tex="+textb+"&lan=zh&per=0&pit=1&spd=5&cuid=8466761&ctp=1&tok="+token
	os.system('mplayer "%s"'%(url))
